package java5.Parent;

import java.util.*;

public class Main {
	public static void main(String[] arge){
		int n=0;
		Scanner in=new Scanner(System.in);
		n=in.nextInt();
		PersonSortable[] object=new PersonSortable[n];
		for(int i=0;i<n;i++)
			object[i]=new PersonSortable (in.next(),in.nextInt());
		Arrays.sort(object,new NameComparator());
		for(int i=0;i<n;i++)
			System.out.println(object[i]);
		Arrays.sort(object,new AgeComparator());
		for(int i=0;i<n;i++)
			System.out.println(object[i]);
		System.out.println(Arrays.toString(NameComparator.class.getInterfaces()));
		System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));
	}
}

class PersonSortable implements Comparable <PersonSortable>{
	private String name;
	private int age;

	
	public PersonSortable(String name,int age){		
			setName(name);
			setAge(age);
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return  name + "-" + age;
	}
	@Override
	public int compareTo(PersonSortable o) {
		// TODO Auto-generated method stub
		return 0;
	}
}

class NameComparator implements Comparator<PersonSortable>{

	{
		System.out.println("NameComparator:sort");
	}
	@Override
	public int compare(PersonSortable o1, PersonSortable o2) {
		if(o1.getName().compareTo(o2.getName())<0) return -1;
		if(o1.getName().compareTo(o2.getName())>0) return 1;
		return 0;
	}
	
}

class AgeComparator implements Comparator<PersonSortable>{

	{
		System.out.println("AgeComparator:sort");
	}
	
	@Override
	public int compare(PersonSortable o1, PersonSortable o2) {
		if(o1.getAge()>o2.getAge())
			return 1;
		if(o1.getAge()<o2.getAge())
			return -1;
		else return 0;
	}
	
}